//
//  HomePageView.m
//  ESPMobile
//
//  Created by Javor on 15/7/22.
//  Copyright (c) 2015年 Javor. All rights reserved.
//

#import "HomePageView.h"
#import "StubObject.h"
#import "Registry.h"
#import "EFMenuCollectionCell.h"
#import "SlideView.h"
#import "SlideViewModel.h"
#import "Constant.h"
#import "ContentViewManager.h"
#import "ViewControllerManager.h"
#import "MainTabBarController.h"
#import "XRCarouselView.h"
#import "EnvironmentVariable.h"
#import "ThemeKit.h"
static NSString * const kHomeTravelsTableViewCell = @"kHomeTravelsTableViewCell";

@implementation HomePageView {
    NSMutableArray *_menuArray;
    EFPageCollectionMenu *_pageCollectionMenu;
    NSMutableArray *_newsArray;
    UIImageView *_backgroundImageView;
    NSString *_newsTitle;
    NSString *_newsURL;
    NSString *_newsDate;
    
    StubObject *_stubObject;
}

- (instancetype)initWithFrame:(CGRect)frame stubObject:(StubObject *)stubObject {
    self = [super initWithFrame:frame];
    if (self) {
        _stubObject = stubObject;
        self.backgroundColor  = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1];
        
        [self createView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame menuArray:(NSMutableArray *)menuArray {
    self = [super initWithFrame:frame];
    if (self) {
        _menuArray = menuArray;
        self.backgroundColor  = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1];
        [self createMenu];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    [self createMenu];
    [self createSlide];
    [self createTheme];
}

-(void)createTheme {
    self.theme_backgroundColor = Theme_BackgroudColor;
}

//初始化菜单
-(void)createMenu{
    if(_stubObject){
        _menuArray = [Registry getRegEntryListWithKey:[_stubObject getID]];
        //分页按钮
        _pageCollectionMenu = [[EFPageCollectionMenu alloc] initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, EF_SCREEN_HEIGHT_MOBLIE - 200) datas:_menuArray];
        _pageCollectionMenu.rowCount = 3;
    }else{
        //分页按钮
        NSInteger row = _menuArray.count % 4 == 0 ? (_menuArray.count/4) : (_menuArray.count/4+1);
        _pageCollectionMenu = [[EFPageCollectionMenu alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, row*120) datas:_menuArray];
        _pageCollectionMenu.rowCount = _menuArray.count % 4 == 0 ? (_menuArray.count/4) : (_menuArray.count/4+1);
    }
    _pageCollectionMenu.columnCount = 4;
    _pageCollectionMenu.pageCollectionMenuDelegate = self;
    _pageCollectionMenu.cellTitleTextColor = [UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1];
    _pageCollectionMenu.cellTitleFontSize = 13;
    _pageCollectionMenu.cellBackgroudColor = [UIColor whiteColor];
    _pageCollectionMenu.currentPageIndicatorTintColor = EF_RED_COLOR;
    [_pageCollectionMenu creationComplete];
    [self addSubview:_pageCollectionMenu];
}

- (void)createSlide {
    NSString *imageNameStr = [_stubObject getStringForKey:@"carouseImage" withDefaultValue:@""];
    if (![imageNameStr isEqualToString:@""]) {
        NSArray *imageName = [imageNameStr componentsSeparatedByString:@"|"];
        NSMutableArray *imageArray = [[NSMutableArray alloc] init];
        for (NSUInteger i = 0; i < imageName.count; i++) {
            NSString *imageFile = [NSString stringWithFormat:@"%@/Documents/%@/Image/%@",NSHomeDirectory(),[EnvironmentVariable getVersionPath], imageName[i]];
            UIImage *image = [UIImage imageWithContentsOfFile:imageFile];
            [imageArray addObject:image];
        }
        XRCarouselView *carouse = [[XRCarouselView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
        carouse.imageArray = imageArray;
        carouse.time = 2;
        [self addSubview:carouse];
    }
}

-(void)pageCollectionMenu:(EFPageCollectionMenu *)pageCollectionMenu didSelectItemAtIndex:(NSInteger)index{
    StubObject *currentStubObject = [pageCollectionMenu.items objectAtIndex:index];
    //根据tabID 判断是跳转tab还是push
    NSString *tabID = [currentStubObject getStringForKey:@"tabID" withDefaultValue:nil];
    if(tabID == nil) {
        //是否隐藏navigationBar
        NSString *hiddenNavBarString = [currentStubObject getStringForKey:@"navigationBarHidden" withDefaultValue:@"false"];
        BOOL hiddenNavBarBool = [hiddenNavBarString isEqualToString:@"true"] ? YES : NO;
        
        UIViewController *vc = [ContentViewManager viewControllerWithStubObject:currentStubObject];
        vc.title = [currentStubObject getCaption];
        vc.hidesBottomBarWhenPushed = YES;
        
        [ViewControllerManager pushViewController:vc animated:YES navigationBarHidden:hiddenNavBarBool];
    }else{
        NSInteger page = -1;
        NSArray *menuArray = [Registry getRegEntryListWithKey:@"menuRoot"];
        StubObject *pageStubObject;
        for (NSUInteger i = 0; i < menuArray.count; i++) {
            pageStubObject = menuArray[i];
            if ([[pageStubObject getID] isEqualToString:tabID]) {
                page = i;
                break;
            }
        }
        if (page > 0) {
            MainTabBarController *tab = [ViewControllerManager getMainTabbarController];
            tab.selectedIndex = page;
        }
    }
}


@end

